
<template xmlns="http://www.w3.org/1999/html">
  <div>
    <div class="search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名" label-width="80px">
          <el-input v-model="username" placeholder="请输入用户名查询" style="width: 200px;margin-right: 10px" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" plain style="margin-left: 10px" @click="select(username)">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table :data="userList.list" strip border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="序号" width="70" align="center" sortable />
        <el-table-column prop="username" label="用户名" width="120" align="center" sortable />
        <el-table-column prop="name" label="姓名" width="120" align="center" sortable />
        <el-table-column prop="phone" label="手机号" width="150" align="center" sortable />
        <el-table-column prop="address" label="地址" width="150" align="center" sortable />
        <el-table-column label="头像" align="center" sortable>
          <template slot-scope="scope">
            <img :src="scope.row.photo" alt="用户头像" style="width: 50px; height: 50px;">
          </template>
        </el-table-column>
        <el-table-column label="用户权限" :formatter="formatRole" />
        <el-table-column>
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row.id,scope.row.roleId)">提升权限</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { deleteAction, getAction, postAction, putAction } from '@/utils/xhs-ajax.js'
export default {
  name: 'UserManager',
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      userList: [],
      username: '',
      name: ''
    }
  },
  created() {
    this.fetchUserList()
  },
  mounted() {
  },
  methods: {
    fetchUserList() {
      getAction('/administrators/findAllUser.do').then(res => {
        this.userList = res.data
        this.total = res.data.total
      })
    }, formatRole(row, column, cellValue) {
      if (row.roleId === 1) {
        return '普通用户'
      } else if (row.roleId === 2) {
        return '管理员'
      }
      return ''
    },
    select(username) {
      getAction('/user/checkLikeUser.do?likeName=' + username).then(res => {
        this.userList = res.data
      })
    },
    reset() {
      this.fetchUserList()
    },
    handleCurrentChange(pageNum) {
      getAction('/administrators/findAllUser.do?pageNum=' + pageNum).then(res => {
        this.userList = res.data
      })
    },
    del(user) {
      if (user.roleId === 1) {
        deleteAction(`/user/deleteUser`, user.id).then(resp => {
          alert('删除成功！')
          this.reset()
        })
      } else {
        this.$message.error('不能删除管理员！')
      }
    },
    handleEdit(id, roleId) {
      console.log(roleId)
      if (roleId === 1) {
        putAction('user/userLevelUp.do?userId=' + id)
        this.reset()
      } else {
        this.$message.error('不能提升管理员的权限！')
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>
